﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace SoundSynthLab.Common
{
    public class CentileConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value.GetType() == typeof(float)) return FloatToInteger((float)value);
            if (value.GetType() == typeof(decimal)) return DecimalToInteger((decimal)value);
            if (value.GetType() == typeof(double)) return DoubleToInteger((double)value);
            if (value.GetType() == typeof(int))
            {
                if (targetType == typeof(float)) return IntegerToFloat((int)value);
                if (targetType == typeof(decimal)) return IntegerToDecimal((int)value);
                if (targetType == typeof(double)) return IntegerToDouble((int)value);

            }
            return 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (targetType == typeof(float)) return IntegerToFloat((double)value);
            if (targetType == typeof(decimal)) return IntegerToDecimal((double)value);
            if (targetType == typeof(double)) return IntegerToDouble((double)value);
            return 1;
        }

        private int FloatToInteger(float value)
        {
            return (int)(value * 100f);
        }

        private int DecimalToInteger(decimal value)
        {
            return (int)(value * (decimal)100);
        }

        private int DoubleToInteger(double value)
        {
            return (int)(value * 100d);
        }


        private float IntegerToFloat(double value)
        {
            return ((float)value) / 100f;
        }

        private decimal IntegerToDecimal(double value)
        {
            return ((decimal)value) / (decimal)100;
        }

        private double IntegerToDouble(double value)
        {
            return ((double)value) / (double)100;
        }
    }

    public class MiliConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value.GetType() == typeof(float)) return FloatToInteger((float)value);
            if (value.GetType() == typeof(decimal)) return DecimalToInteger((decimal)value);
            if (value.GetType() == typeof(double)) return DoubleToInteger((double)value);
            if (value.GetType() == typeof(int))
            {
                if (targetType == typeof(float)) return IntegerToFloat((int)value);
                if (targetType == typeof(decimal)) return IntegerToDecimal((int)value);
                if (targetType == typeof(double)) return IntegerToDouble((int)value);
            
            }
            return 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
                if (targetType == typeof(float)) return IntegerToFloat((double)value);
                if (targetType == typeof(decimal)) return IntegerToDecimal((double)value);
                if (targetType == typeof(double)) return IntegerToDouble((double)value);
                return 1;
        }

        private int FloatToInteger(float value)
        {
            return (int)(value * 1000f);
        }

        private int DecimalToInteger(decimal value)
        {
            return (int)(value * (decimal)1000);
        }

        private int DoubleToInteger(double value)
        {
            return (int)(value * 1000d);
        }


        private float IntegerToFloat(double value)
        {
            return ((float)value)/1000f;
        }

        private decimal IntegerToDecimal(double value)
        {
            return ((decimal)value) / (decimal)1000;
        }

        private double IntegerToDouble(double value)
        {
            return ((double)value) / (double)1000;
        }
    }
}

